package com.crm.service.impl;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.crm.commons.exception.BusinessException;
import com.crm.entity.CrmMenu;
import com.crm.entity.CrmUser;
import com.crm.mapper.CrmMenuMapper;
import com.crm.service.ICrmMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.crm.vo.MySecurityUser;
import com.crm.vo.SysMenu;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

import static com.crm.commons.exception.enumVo.BussinessExceptionEnum.MENU_HAS_CHILD;
import static com.crm.commons.exception.enumVo.BussinessExceptionEnum.MENU_NOT_EXISTS;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author junxian, dou
 * @since 2024-11-28
 */
@Slf4j
@Service
public class CrmMenuServiceImpl extends ServiceImpl<CrmMenuMapper, CrmMenu> implements ICrmMenuService {


    @Override
    public List<SysMenu> getListToNodeTree(CrmUser crmUser) {

        List<CrmMenu> list = baseMapper.selectLsitByUserId(crmUser.getId());
        List<SysMenu> resultMenuList = new ArrayList<>();
        for (CrmMenu crmMenu : list) {
            SysMenu sysMenu = new SysMenu(crmMenu);
            // 寻找子节点
            for (CrmMenu e : list) {
                if (e.getParentId() == crmMenu.getId()) {
                    SysMenu child = new SysMenu(e);
                    sysMenu.getChildren().add(child);
                }
            }
            if (crmMenu.getParentId() == 0L) {
                resultMenuList.add(sysMenu);
            }
        }
        return resultMenuList;
    }

    @Override
    public List<SysMenu> getListToNodeTree() {
        LambdaQueryWrapper<CrmMenu> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByAsc(CrmMenu::getOrderNum);
        List<CrmMenu> list = list();
        SysMenu sysMenu = new SysMenu();
        sysMenu.setId(0l);
        sysMenu.setName("root");

        return findChildren(sysMenu, list).getChildren();
    }

    @Override
    public void saveMenu(SysMenu sysMenu) {
        if (sysMenu.getId() == null || sysMenu.getId() == -1) {
            CrmMenu crmMenu = sysMenu.getCrmMenu();
            crmMenu.setId(null);
            save(crmMenu);
        } else {

            updateById(sysMenu.getCrmMenu());
        }
    }

    @Override
    public SysMenu getMenuById(Long id) {
        CrmMenu byId = getById(id);
        if (ObjectUtil.isNull(byId)) {
            throw new BusinessException(MENU_NOT_EXISTS);
        }
        return new SysMenu(byId);
    }

    private SysMenu findChildren(SysMenu sysMenus, List<CrmMenu> list) {
        for (CrmMenu crmMenu : list) {
            // 寻找子节点
            SysMenu child = new SysMenu(crmMenu);
            if (crmMenu.getParentId() == sysMenus.getId()) {
                if (sysMenus.getChildren() == null) {
                    sysMenus.setChildren(new ArrayList<>());
                }
                sysMenus.getChildren().add(findChildren(child, list));
            }
        }
        return sysMenus;
    }

    @Override
    public void deleteById(Long id) {
        LambdaQueryWrapper<CrmMenu> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CrmMenu::getParentId,id);
        long count = count(wrapper);
        if(count>0){
            throw new BusinessException(MENU_HAS_CHILD);
        }
        removeById(id);
    }

    @Override
    public List<String> selectIconList() {
        File file = new File("D:\\porsche\\springboot-crm\\crm_ui\\crm\\src\\icons\\svg");

        File[] files = file.listFiles();

        int length = files.length;
        log.info(String.valueOf(length));
       return Arrays.stream(files).map(new Function<File, String>() {
            @Override
            public String apply(File file) {
               return FileUtil.mainName(file);
            }
        }).collect(Collectors.toList());
    }
}
